package cn.tedu._06vehicle.base.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;

@Slf4j
public class TimeInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入限定访问时间的拦截器...");
        //获取当前时间，再判断
        LocalDateTime now = LocalDateTime.now();//获取当前时间的对象
        int hour=now.getHour();//获取当前时间的小时
        if (hour<6 || hour>22){
            //不允许访问：抛出异常-->定义异常处理方法-->返回JsonResult
            throw new RuntimeException("当前时间无法访问，请在6-23点之间访问");
        }
        //允许访问
        return true;

        //登录状态验证

    }
}
